package com.chromatic.competition.model;

import android.content.Context;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;

import com.chromatic.competition.repository.ChatRepository;

import java.util.List;

import okhttp3.OkHttpClient;

public class ChatViewModel extends ViewModel {
    private final ChatRepository chatRepository;
    private final MutableLiveData<List<UserMessage>> messagesLiveData = new MutableLiveData<>();

    public ChatViewModel(Context context) {
        OkHttpClient client = new OkHttpClient();
        chatRepository = new ChatRepository(context, client);
    }
    public void sendMessage(int friendId, String message) {
        chatRepository.sendMessage(friendId, message);
        loadMessages(friendId);
    }
    public void loadMessages(int friendId) {
        chatRepository.getMessages(friendId).observeForever(new Observer<List<UserMessage>>() {
            @Override
            public void onChanged(List<UserMessage> userMessages) {
                messagesLiveData.setValue(userMessages);
            }
        });
    }
    public LiveData<List<UserMessage>> getMessagesLiveData() {
        return messagesLiveData;
    }
}

